const xhr = new XMLHttpRequest();
var words = document.querySelector("#show_words_box");
var show_explain_row = document.querySelector("#show_explain_row");
var explain = document.querySelector("#show_explain_box");
var last_one = document.querySelector("#last_one");
var obscure = document.querySelector("#obscure");
var next_one = document.querySelector("#next_one");
var cover_layer = document.querySelector("#cover_layer");
var label_box=document.querySelector("#label_box");
var goal = document.querySelector("#goal");
var upload=document.querySelector("#upload");
var progress_c=document.querySelector("#progress_c");
var familiar=document.querySelector("#familiar");
var netsite = "http://39.103.159.52:6607";
//"http://127.0.0.1:6607";
//"http://39.103.159.52:6607";
var vacabulary={};
var aGroupNumber=10;
var aimto=10;
var count=0;
var today=new Date();
year=today.getFullYear();
month=today.getMonth() + 1;
day=today.getDate();
console.log(year,month,day,today);
var list =new Array();
var oneday=86400000;
var passcount=0;
var aisihaobin = [0,oneday,2*oneday,4*oneday,7*oneday,15*oneday]
var vblen=0;
var everydayAim=50;
function countaimNumber(vacabulary,vblen,dayAim){
    let j=0;
    for(let i = 0;i<vblen;i++){
        if(vacabulary[i]["year"]==year&&vacabulary[i]["month"]==month&&vacabulary[i]["day"]==day&&vacabulary[i]["remeber"]=="1"){
            dayAim--;
            console.log(dayAim,vacabulary[i]);
        }else if(vacabulary[i]["remeber"]=="0"){
            console.log(j,vacabulary[i]);
            j++;
        }
        if(dayAim<=0||j>=50){
            return dayAim;
        }
    }
    return dayAim;
}
function show_Aimlabel(){
    let Aim=countaimNumber(vacabulary,vblen,everydayAim)
    if(Aim==0){
        label_box.innerHTML="今日目标已完成"
    }else{
        label_box.innerHTML="今日目标:"+Aim;
    }

}
function get_vacabulary() {
    // goal.disable=true;
    xhr.open('POST', netsite + '/vacabulary_get');
    xhr.send();
    xhr.onreadystatechange = function () {
        console.log(xhr.readyState);
        if (xhr.readyState === 4) {
            console.log(xhr.status);
            if (xhr.status >= 200 && xhr.status < 300) {
                if(xhr.response=="pls login first!!"){
                    alert("请登录");
                }
                // console.log(xhr.response);
                vacabulary = JSON.parse(xhr.response);
                vblen=Object.getOwnPropertyNames(vacabulary).length-2;
                show_Aimlabel();
                // goal.disable=false;
            }
        }
    }
    return vacabulary;
}
function initial(i){
    vacabulary[i]["repeat"]=0;
    vacabulary[i]["num"]=i;
    if(!vacabulary[i]["month"]){
        vacabulary[i]["month"]=0
    }
    if(!vacabulary[i]["day"]){
        vacabulary[i]["day"]=0
    }
    if(!vacabulary[i]["year"]){
        vacabulary[i]["year"]=0
    }
    if(!vacabulary[i]["times"]){
        vacabulary[i]["times"]=0
    }
    if(!vacabulary[i]["remeber"]){
        vacabulary[i]["remeber"]=0
    }
}
function create_list(){
    console.log(vblen);
    list =[];
    for(let i = 0;i<vblen;i++){
        initial(i);
        let aday = new Date(vacabulary[i]["year"],vacabulary[i]["month"]-1,vacabulary[i]["day"]);
        console.log(vacabulary[i]["year"],vacabulary[i]["month"],vacabulary[i]["day"],aday);
        console.log(today-aisihaobin[vacabulary[i]["remeber"]],today-aisihaobin[vacabulary[i]["remeber"]]-aday);
        if(vacabulary[i]["remeber"] < 6 && today-aisihaobin[vacabulary[i]["remeber"]] > aday){
            list[list.length]=vacabulary[i];
            console.log("listmemeber",list[list.length-1]);
            if(list.length>=aimto){
                count=0;
                passcount=0;
                current_progress();
                return;
            }
        }
    }
}
function sendmark(mark) {
    xhr.open('POST', netsite + '/vacabulary_update');
    xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
    xhr.send(mark);
}
function wordFontSize(str){
    if(str.length<=20){
        words.style.fontSize="2.8571rem";
    }else if(str.length>20 && str.length <=40){
        words.style.fontSize="2.5rem";
    }else if(str.length>40){
        words.style.fontSize="2.1429rem";
    }
}
function explainFontSize(str){
    if(str.length<=30){
        explain.style.fontSize="1.4286rem";
    }else if(str.length>30 && str.length <=70){
        explain.style.fontSize="1.2143rem";
    }else if(str.length>70){
        explain.style.fontSize="1.0714rem";
    }
}
function show_vacabulary() {
    if(count!=-1){
        let str=vacabulary[list[count]["num"]]["words"];
        wordFontSize(str);
        words.innerHTML = vacabulary[list[count]["num"]]["words"];
        explain.innerHTML = "";
    }else{
        words.innerHTML ="";
        explain.innerHTML = "单词表已完成,请双击保存数据";
    }
}

function current_progress(){
    rate=100*(passcount/list.length);
    progress_c.style.width=rate+"%";
}

function setgoal() {
    aimto=parseInt(goal.value);
    console.log(aimto,vblen);
    create_list();
    show_vacabulary();
}

function show_means(){
    let str=vacabulary[list[count]["num"]]["means"];
    explainFontSize(str);
    if(vacabulary[list[count]["num"]]["times"]){
        let times=vacabulary[list[count]["num"]]["times"]+1;
        explain.innerHTML = vacabulary[list[count]["num"]]["means"] + '\n***当前第'+times+'次***';
    }else{
        explain.innerHTML = vacabulary[list[count]["num"]]["means"];
    }
}
function pass(){
    // 修改记忆住的次数
    if(list[count]["repeat"]==2){
        console.log("count",count);
        passcount++;
        console.log(list[count]["times"],vacabulary[list[count]["num"]]["times"]);
        // 竟然是堆区的数据，list 与 vacabulary公用一套数据
        list[count]["times"]=list[count]["times"]+1;
        list[count]["remeber"]=list[count]["remeber"]+1;
        list[count]["year"]=year;
        list[count]["month"]=month;
        list[count]["day"]=day;
        // console.log("sli1",count,list.length);
        // delete list[count];
        // for(let i = count ; i < list.length; i++ ){
        //     list[i]=list[i+1];
        // }
        // list.length=list.length-1;
        // count--;
        current_progress();
        console.log(list[count]["times"],vacabulary[list[count]["num"]]["times"]);
    }
    // 计数
    list[count]["repeat"]=list[count]["repeat"]+1;
}
get_vacabulary();
var countgroupNumber=1;
function coutplus(count){
    for(let i=0; i<list.length;i++){
        if(count<list.length-1){
            count++;
        }else{
            count=0;
        }
        if(countgroupNumber==aGroupNumber){
            countgroupNumber=0;
            count=0;
            i=0;
        }
        if(list[count]["repeat"]<3){
            countgroupNumber++;
            return count;
        }
    }
    count=-1;
    return count;
}

next_one.addEventListener("click", function () {
    if (explain.innerHTML == "") {
        show_means();
    } else {
        if(count!=-1){
            pass();
            count=coutplus(count);
            show_vacabulary();
        }else{
            words.innerHTML ="";
            explain.innerHTML = "单词表已完成,请双击保存数据";
        }
    }
})

obscure.addEventListener("click", function () {
    if (explain.innerHTML == "") {
        show_means();
    } else {
        if(count!=-1){
            if(list[count]["repeat"]<2){
                list[count]["repeat"]=0;
            }
            count=coutplus(count);
            show_vacabulary();
        }else{
            words.innerHTML ="";
            explain.innerHTML = "单词表已完成,请双击保存数据";
        }
    }
})

last_one.addEventListener("click", function () {
    if(count!=-1){
        if(count>0){
            count--;
        }else{
            count=list.length-1;
        }
        show_vacabulary();
    }else{
        explain.innerHTML = "单词表已完成,请双击保存数据";
    }
})
familiar.addEventListener("click", function () {
    if (explain.innerHTML == "") {
        show_means();
    } else {
        if(count!=-1){
            if(list[count]["repeat"]<2){
                list[count]["repeat"]=2;
            }
            pass();
            count=coutplus(count);
            show_vacabulary();
        }else{
            words.innerHTML ="";
            explain.innerHTML = "单词表已完成,请双击保存数据";
        }
    }
})

show_explain_row.addEventListener("dblclick", function () {
    cover_layer.style.display = "block";
    show_Aimlabel();
    console.log(list);
    sendmark(JSON.stringify(list));
})

cover_layer.addEventListener("dblclick", function () {
    if (parseInt(goal.value)) {
        cover_layer.style.display="none";
        setgoal()
    }else{
        goal.value ="..";
    }
})